package com.zzvcom.webapp.action.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.zzvcom.persist.model.admin.Image;
import com.zzvcom.persist.model.admin.LogImg;
import com.zzvcom.persist.model.admin.ProductDes;
import com.zzvcom.service.admin.GalleryManager;
import com.zzvcom.service.admin.ImageManager;
import com.zzvcom.service.admin.LogImgManager;
import com.zzvcom.service.admin.ProductDesManager;
import com.zzvcom.service.admin.ProductManager;
import com.zzvcom.template.ResultPage;
import com.zzvcom.utils.ExtMessage;
import com.zzvcom.utils.IMGTools;
import com.zzvcom.webapp.action.IndexAction;
import com.zzvcom.webapp.action.indexbean.LeftList;

@Controller
@Scope("prototype")
public class LogImgAction
{

    private LogImg log;

    private LogImgManager manager;

    private ExtMessage msg = new ExtMessage();// 返回信息

    private Map rs = new HashMap();// 结果集合

    private ResultPage resultPage;

    private Integer start;

    private Integer limit;

    // do other thing manager

    private GalleryManager calleryManager;

    private ProductManager productManager;

    private ImageManager imageManager;
    
    private ProductDesManager productDesManager;

    public String execute()
    {
        if (resultPage == null)
        {
            resultPage = new ResultPage();
        }
        resultPage.startLimitSetting(start, limit);
        DetachedCriteria dc = DetachedCriteria.forClass(LogImg.class);
        dc.addOrder(Order.desc("seq"));
        resultPage.setDetachedCriteria(dc);
        manager.getResultPageByDetachedCriteria(resultPage);
        rs.put("root", resultPage.getResult());
        rs.put("records", resultPage.getTotalRecord());
        return "rs";
    }

    public String save()
    {
        if (StringUtils.isBlank(log.getId()))
        {
            log.setId(null);
        }
        manager.save(log);
        msg.setInfo("保存成功");
        return "msg";
    }

    public String clearLeftMenuCache()
    {
        LeftList.clear();// clear the cache
        new LeftList();// count the cache
        IndexAction.clearCache();
        msg.setInfo("缓存清理成功!");
        return "msg";
    }

    public String clearImage()
    {
        ResultPage imagePage=new ResultPage();
        imagePage.setPageSize(20);
        DetachedCriteria dc=DetachedCriteria.forClass(Image.class);
        imagePage.setDetachedCriteria(dc);
        imageManager.getResultPageByDetachedCriteria(imagePage);
        long count=0;
        //System.out.println("total page:"+imagePage.getTotalPage());
        for(int i=1;i<=imagePage.getTotalPage();i++)
        {
            for(Image aImage:(List<Image>) imageManager.queryByHQL("from Image", (i-1)*imagePage.getPageSize(), imagePage.getPageSize()))
            {
                count=count+Long.valueOf(imageManager.queryUniqueByHQL("select count(*) from LogImg where image=? ", aImage.getId()).toString());
                count=count+Long.valueOf(imageManager.queryUniqueByHQL("select count(*) from Gallery where image=? ", aImage.getId()).toString());
                count=count+Long.valueOf(imageManager.queryUniqueByHQL("select count(*) from Product where image=? ", aImage.getId()).toString());
                
                if(count==0)
                {
                    imageManager.deleteImageById(aImage.getId());
                }
                
                count=0;             
               
            }            
            //System.out.println("page:"+i);        
        }
        
        
        
        msg.setInfo("垃圾图片成功!");
        return "msg";
    }
    
    public String recoverImgSrc()
    {
        ResultPage cPage=new ResultPage();
        cPage.setPageSize(20);
        DetachedCriteria dc=DetachedCriteria.forClass(ProductDes.class);
        cPage.setDetachedCriteria(dc);
        productDesManager.getResultPageByDetachedCriteria(cPage);
        for(int i=1;i<=cPage.getTotalPage();i++)
        {
            for(ProductDes des:(List<ProductDes>) productDesManager.queryByHQL("from ProductDes", (i-1)*cPage.getPageSize(), cPage.getPageSize()))
            {
                des.setContent(IMGTools.imgSrc(des.getContent()));
                productDesManager.save(des);
            }
        }
        msg.setInfo("图片链接恢复成功!");
        return "msg";
    }    
    
    public String skypeAndMsn()
    {
        ResultPage cPage=new ResultPage();
        cPage.setPageSize(20);
        DetachedCriteria dc=DetachedCriteria.forClass(ProductDes.class);
        cPage.setDetachedCriteria(dc);
        productDesManager.getResultPageByDetachedCriteria(cPage);
        for(int i=1;i<=cPage.getTotalPage();i++)
        {
            for(ProductDes des:(List<ProductDes>) productDesManager.queryByHQL("from ProductDes", (i-1)*cPage.getPageSize(), cPage.getPageSize()))
            {
                des.setContent(IMGTools.skypeAndMsn(des.getContent()));
                productDesManager.save(des);
            }
        }
        msg.setInfo("图片链接恢复成功!");
        return "msg";
    }    

    public String delete()
    {
        log = manager.get(log.getId());
        manager.delete(log);
        msg.setInfo("删除成功");
        return "msg";
    }

    public ResultPage getResultPage()
    {
        return resultPage;
    }

    public void setResultPage(ResultPage resultPage)
    {
        this.resultPage = resultPage;
    }

    public LogImg getLog()
    {
        return log;
    }

    public void setLog(LogImg log)
    {
        this.log = log;
    }

    public LogImgManager getManager()
    {
        return manager;
    }

    @Autowired
    public void setManager(LogImgManager manager)
    {
        this.manager = manager;
    }

    public ExtMessage getMsg()
    {
        return msg;
    }

    public void setMsg(ExtMessage msg)
    {
        this.msg = msg;
    }

    public Map getRs()
    {
        return rs;
    }

    public void setRs(Map rs)
    {
        this.rs = rs;
    }

    public Integer getStart()
    {
        return start;
    }

    public void setStart(Integer start)
    {
        this.start = start;
    }

    public Integer getLimit()
    {
        return limit;
    }

    public void setLimit(Integer limit)
    {
        this.limit = limit;
    }

    public GalleryManager getCalleryManager()
    {
        return calleryManager;
    }

    @Autowired
    public void setCalleryManager(GalleryManager calleryManager)
    {
        this.calleryManager = calleryManager;
    }

    public ProductManager getProductManager()
    {
        return productManager;
    }

    @Autowired
    public void setProductManager(ProductManager productManager)
    {
        this.productManager = productManager;
    }

    public ImageManager getImageManager()
    {
        return imageManager;
    }

    @Autowired
    public void setImageManager(ImageManager imageManager)
    {
        this.imageManager = imageManager;
    }

    public ProductDesManager getProductDesManager()
    {
        return productDesManager;
    }
    @Autowired
    public void setProductDesManager(ProductDesManager productDesManager)
    {
        this.productDesManager = productDesManager;
    }

}
